package com.demostic.demostic_test.fragment;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.cloudservice.VoidCallback;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACException;
import com.accloud.service.ACUserDevice;
import com.accloud.utils.LogUtil;
import com.badoo.mobile.util.WeakHandler;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.activity.LoginActivity;
//import com.demostic.demostic_test.activity.MapActivity_430;
//import com.demostic.demostic_test.activity.MapActivity_600;
//import com.demostic.demostic_test.activity.MapActivity_786;
//import com.demostic.demostic_test.activity.MapActivity_790_;
//import com.demostic.demostic_test.activity.MapActivity_800;
//import com.demostic.demostic_test.activity.MapActivity_901;
//import com.demostic.demostic_test.activity.MapActivity_x7;
//import com.demostic.demostic_test.activity.SelectActivity_;
import com.demostic.demostic_test.activity.MapActivity_786;
import com.demostic.demostic_test.activity.MapActivity_901;
import com.demostic.demostic_test.activity.MapEditActivity901;
import com.demostic.demostic_test.adapter.DevListAdapter_new;
import com.demostic.demostic_test.entity.DevicePropertyInfo_;
import com.demostic.demostic_test.utils.Constants;
import com.demostic.demostic_test.utils.DialogUtils;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.SpUtils;
import com.demostic.demostic_test.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static androidx.swiperefreshlayout.widget.SwipeRefreshLayout.*;

public class DeviceFragment extends Fragment implements View.OnClickListener, OnRefreshListener {
    public static final String TAG = DeviceFragment.class.getSimpleName();
    public static boolean isStop;
    private final int REFRESH_OVER = 17;
    private DevListAdapter_new adapter;
    private Dialog alertDialog;
    private Button bt_add;
    private Context context;
    private int devCount = 0;
    private ACDeviceMsg deviceMsg;
    private Dialog dialog;
    private RelativeLayout fgm_dev_relative;
    private Gson gson;
    private WeakHandler handler = new WeakHandler(new Handler.Callback() {
        /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass1 */

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 17:
                    DeviceFragment.this.refreshLayout.setRefreshing(false);
                    break;
            }
            return false;
        }
    });
    private RecyclerView listView;
    public List<ACUserDevice> mAcUserDevices;
    public ThreadPoolExecutor poolExecutor;
    private SwipeRefreshLayout refreshLayout;
    private TextView textView1;
    private TextView tv_notice;
    private int unbindPosition;

    static /* synthetic */ int access$1108(DeviceFragment x0) {
        int i = x0.devCount;
        x0.devCount = i + 1;
        return i;
    }

    public View onCreateView(LayoutInflater inflater, @NonNull ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device, (ViewGroup) null);
        init(view);
        this.deviceMsg = new ACDeviceMsg(65, new byte[]{0});
        this.poolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS, new LinkedBlockingDeque(128));
        return view;
    }

    @Override // android.support.v4.app.Fragment
    public void onResume() {
        super.onResume();
        isStop = false;
        initDialog();
        initList();
        AC.deviceDataMgr().unSubscribeAllProperty();
        AC.classDataMgr().unSubscribeAll();
    }

    private void initDialog() {
        this.dialog = DialogUtils.createLoadingDialog_(this.context);
        this.dialog.show();
    }

    private void setListener() {
        this.refreshLayout.setOnRefreshListener(this);
        this.bt_add.setOnClickListener(this);
    }

    private void init(View view) {
        this.context = getActivity();
        this.gson = new Gson();
        this.mAcUserDevices = new ArrayList();
        this.tv_notice = (TextView) view.findViewById(R.id.tv_notice);
        this.bt_add = (Button) view.findViewById(R.id.bt_add);
        this.listView = (RecyclerView) view.findViewById(R.id.listView);
        this.adapter = new DevListAdapter_new(this.context, this.mAcUserDevices);
        this.adapter.setOnClickListener(new DevListAdapter_new.OnClickListener() {
            /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass2 */

            @Override // com.demostic.demostic_test.adapter.DevListAdapter_new.OnClickListener
            public void onMenuClick(int position) {
                DeviceFragment.this.unbindPosition = position;
                View view_unbind = LayoutInflater.from(DeviceFragment.this.context).inflate(R.layout.unbind_dialog, (ViewGroup) null);
                DeviceFragment.this.alertDialog = DialogUtils.showUnifyDialog(DeviceFragment.this.context, view_unbind);
                DeviceFragment.this.initUnbindView(view_unbind);
                DeviceFragment.this.alertDialog.show();
            }

            @Override // com.demostic.demostic_test.adapter.DevListAdapter_new.OnClickListener
            public void onContentClick(int position) {
                if (DeviceFragment.this.isDevOnline(DeviceFragment.this.mAcUserDevices, position)) {
                    String subdomain = DeviceFragment.this.mAcUserDevices.get(position).getSubDomain();
                    SpUtils.saveString(DeviceFragment.this.context, "physicalDeviceId", DeviceFragment.this.mAcUserDevices.get(position).getPhysicalDeviceId());
                    SpUtils.saveString(DeviceFragment.this.context, "subdomain", subdomain);
                    SpUtils.saveString(DeviceFragment.this.context, "devName", DeviceFragment.this.mAcUserDevices.get(position).getName());
                    SpUtils.saveLong(DeviceFragment.this.context, "deviceId", DeviceFragment.this.mAcUserDevices.get(position).getDeviceId());
                    SpUtils.saveLong(DeviceFragment.this.context, "owner", DeviceFragment.this.mAcUserDevices.get(position).getOwner());
                    MyLog.e(DeviceFragment.TAG, "DeviceFragment111 onContentClick owner = " + DeviceFragment.this.mAcUserDevices.get(position).getOwner() + "<->" + DeviceFragment.this.mAcUserDevices.get(position).toString());
                    if (subdomain.equals("ilifex900") || subdomain.equals(Constants.subdomain_x901) || subdomain.equals("ilifex900")) {
                        DeviceFragment.this.startActivity(new Intent(DeviceFragment.this.getActivity(), MapActivity_901.class));

//                        Intent intent = new Intent(getActivity(), MapEditActivity901.class);
//                        intent.putExtra("EDIT_TAG", 3);
//                        DeviceFragment.this.startActivity(intent);
                    } else{
                        DeviceFragment.this.startActivity(new Intent(DeviceFragment.this.getActivity(), MapActivity_786.class));
                    }
                } else {
                    DeviceFragment.this.showOfflineDialog();
                }
            }
        });
        this.listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        this.listView.setAdapter(this.adapter);
        this.listView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass3 */

            @Override // android.support.v7.widget.RecyclerView.OnScrollListener
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                DeviceFragment.this.adapter.setScrollingMenu(null);
            }
        });
        this.fgm_dev_relative = (RelativeLayout) view.findViewById(R.id.fgm_dev_relative);
        this.refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refreshLayout);
        this.refreshLayout.setColorSchemeColors(getResources().getColor(17170459), getResources().getColor(17170452), getResources().getColor(17170456), getResources().getColor(17170454));
        setListener();
    }

    public void initUnbindView(View view) {
        ((TextView) view.findViewById(R.id.tv_dialog_cancel)).setOnClickListener(new UnbindListener());
        ((TextView) view.findViewById(R.id.tv_dialog_confirm)).setOnClickListener(new UnbindListener());
    }

    public class UnbindListener implements View.OnClickListener {
        public UnbindListener() {
        }

        @Override // android.view.View.OnClickListener
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_dialog_cancel /* 2131755504 */:
                    DeviceFragment.this.alertDialog.dismiss();
                    return;
                case R.id.tv_dialog_confirm /* 2131755505 */:
                    DeviceFragment.this.alertDialog.dismiss();
                    DeviceFragment.this.dialog.show();
                    AC.bindMgr().unbindDevice(DeviceFragment.this.mAcUserDevices.get(DeviceFragment.this.unbindPosition).getSubDomain(), DeviceFragment.this.mAcUserDevices.get(DeviceFragment.this.unbindPosition).deviceId, new VoidCallback() {
                        /* class com.demostic.demostic_test.fragment.DeviceFragment.UnbindListener.AnonymousClass1 */

                        @Override // com.accloud.cloudservice.VoidCallback
                        public void success() {
                            DeviceFragment.this.mAcUserDevices.remove(DeviceFragment.this.unbindPosition);
                            DeviceFragment.this.adapter.notifyDataSetChanged();
                            if (DeviceFragment.this.mAcUserDevices.size() == 0) {
                                DeviceFragment.this.showButton();
                            }
                            DialogUtils.closeDialog(DeviceFragment.this.dialog);
                        }

                        @Override // com.accloud.cloudservice.BaseCallback
                        public void error(ACException e) {
                            MyLog.e(DeviceFragment.TAG, "unbindPosition===:" + e.toString());
                            ToastUtils.showToast(DeviceFragment.this.context, DeviceFragment.this.getString(R.string.dev_fgm_unbind_fail));
                            DialogUtils.closeDialog(DeviceFragment.this.dialog);
                        }
                    });
                    return;
                default:
                    return;
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showOfflineDialog() {
        View view = LayoutInflater.from(this.context).inflate(R.layout.offline_dialog, (ViewGroup) null);
        Dialog dialog2 = DialogUtils.showUnifyDialog(this.context, view);
        initOfflineView(view, dialog2);
        dialog2.show();
    }

    private void initOfflineView(View view, final Dialog dialog2) {
        ((TextView) view.findViewById(R.id.tv_ok)).setOnClickListener(new View.OnClickListener() {
            /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass4 */

            @Override // android.view.View.OnClickListener
            public void onClick(View v) {
                if (dialog2 != null && dialog2.isShowing()) {
                    dialog2.dismiss();
                    DeviceFragment.this.listDevices();
                }
            }
        });
    }

    public void initList() {
        if (!AC.accountMgr().isLogin()) {
            showButton();
        } else {
            listDevices();
        }
    }

    public boolean isDevOnline(List<ACUserDevice> mAcUserDevices2, int position) {
        if (mAcUserDevices2.get(position).getStatus() == 0) {
            return false;
        }
        return true;
    }

    public void showList() {
        DialogUtils.closeDialog(this.dialog);
        this.bt_add.setVisibility(8);
        this.tv_notice.setVisibility(8);
        this.refreshLayout.setVisibility(0);
        this.listView.setVisibility(0);
        this.adapter.notifyDataSetChanged();
        for ( int i = 0; i < this.mAcUserDevices.size(); i++) {
            final int mI = i;
            this.poolExecutor.execute(new Runnable() {
                /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass5 */

                @Override // java.lang.Runnable
                public void run() {
                    DeviceFragment.this.getDeviceStatus(DeviceFragment.this.deviceMsg, DeviceFragment.this.mAcUserDevices.get(mI).physicalDeviceId, DeviceFragment.this.mAcUserDevices.get(mI).getSubDomain());
                }
            });
        }
        if (this.devCount >= this.mAcUserDevices.size()) {
            this.adapter.notifyDataSetChanged();
        }
    }

    public void showButton() {
        DialogUtils.closeDialog(this.dialog);
        this.refreshLayout.setVisibility(8);
        this.listView.setVisibility(8);
        this.bt_add.setVisibility(0);
        this.tv_notice.setVisibility(0);
    }

    @Override // android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener
    public void onRefresh() {
        listDevices();
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_add /* 2131755601 */:
                if (!AC.accountMgr().isLogin()) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    return;
                } else {
//                    startActivity(new Intent(getActivity(), SelectActivity_.class));
                    return;
                }
            default:
                return;
        }
    }

    @Override // android.support.v4.app.Fragment
    public void onStop() {
        super.onStop();
        isStop = true;
    }

    public void listDevices() {
        AC.bindMgr().listDevicesWithStatus(new PayloadCallback<List<ACUserDevice>>() {
            /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass6 */

            public void success(List<ACUserDevice> acUserDevices) {
                MyLog.e(DeviceFragment.TAG, "listDevicesWithStatus success===:" + acUserDevices.size());
                DeviceFragment.this.mAcUserDevices.clear();
                if (acUserDevices.size() == 0) {
                    DeviceFragment.this.showButton();
                } else {
                    for (ACUserDevice acUserDevice : acUserDevices) {
                        MyLog.e(DeviceFragment.TAG, "listDevicesWithStatus success:" + acUserDevice.getSubDomain());
                        DeviceFragment.this.mAcUserDevices.add(acUserDevice);
                    }
                    DeviceFragment.this.showList();
                    DeviceFragment.this.saveDeviceType();
                }
                if (DeviceFragment.this.refreshLayout.isRefreshing()) {
                    DeviceFragment.this.handler.sendEmptyMessageDelayed(17, 500);
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(DeviceFragment.TAG, "listDevicesWithStatus error===:" + e.toString());
                if (DeviceFragment.this.mAcUserDevices.size() > 0) {
                    DeviceFragment.this.showList();
                } else {
                    DeviceFragment.this.showButton();
                }
                if (DeviceFragment.this.refreshLayout.isRefreshing()) {
                    DeviceFragment.this.handler.sendEmptyMessageDelayed(17, 500);
                }
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void saveDeviceType() {
        for (int i = 0; i < this.mAcUserDevices.size(); i++) {
            String domain = this.mAcUserDevices.get(i).getSubDomain();
            long deviceId = this.mAcUserDevices.get(i).getDeviceId();
            final String physicalid = this.mAcUserDevices.get(i).getPhysicalDeviceId();
            if (domain.equals(Constants.subdomain_x800)) {
                AC.deviceDataMgr().fetchCurrentProperty(domain, deviceId, new PayloadCallback<String>() {
                    /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass7 */

                    public void success(String s) {
                        SpUtils.saveInt(DeviceFragment.this.context, "devType" + physicalid, ((DevicePropertyInfo_) DeviceFragment.this.gson.fromJson(s,  DevicePropertyInfo_.class)).getDevice_type());
                    }

                    @Override // com.accloud.cloudservice.BaseCallback
                    public void error(ACException e) {
                        ToastUtils.showErrorToast(DeviceFragment.this.context, e.getErrorCode());
                    }
                });
            }
        }
    }

    @Override // android.support.v4.app.Fragment
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if ((!hidden) && AC.accountMgr().isLogin()) {
            listDevices();
        }
    }

    public void getDeviceStatus(ACDeviceMsg deviceMsg2, final String physicalDeviceId, String subdomain) {
        AC.bindMgr().sendToDeviceWithOption(subdomain, physicalDeviceId, deviceMsg2, 2, new PayloadCallback<ACDeviceMsg>() {
            /* class com.demostic.demostic_test.fragment.DeviceFragment.AnonymousClass8 */

            public void success(ACDeviceMsg acDeviceMsg) {
                int i = 1;
                MyLog.e(DeviceFragment.TAG, "getDeviceStatus success===:" + acDeviceMsg.toString());
                DeviceFragment.access$1108(DeviceFragment.this);
                if (!DeviceFragment.isStop) {
                    boolean isOnline = acDeviceMsg.getContent()[0] != 0;
                    ACUserDevice device = DeviceFragment.this.getACUserDeviceById(physicalDeviceId);
                    if (device != null) {
                        if (!isOnline) {
                            i = 0;
                        }
                        device.setStatus(i);
                    }
                }
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                ACUserDevice device;
                MyLog.e(DeviceFragment.TAG, "getDeviceStatus error===:" + e.toString());
                DeviceFragment.access$1108(DeviceFragment.this);
                if (!DeviceFragment.isStop && (device = DeviceFragment.this.getACUserDeviceById(physicalDeviceId)) != null && e.getErrorCode() == 3807) {
                    device.setStatus(0);
                }
            }
        });
    }

    public ACUserDevice getACUserDeviceById(String physicalDeviceId) {
        for (int i = 0; i < this.mAcUserDevices.size(); i++) {
            if (this.mAcUserDevices.get(i).physicalDeviceId.equals(physicalDeviceId)) {
                return this.mAcUserDevices.get(i);
            }
        }
        return null;
    }
}
